//index.js
//获取应用实例
const app = getApp();
// var template = require('../header/header.js');

Page({
    data: {
        nav_list: [
            {name: '用户名', text: ''},
            {name: '真实姓名', text: ''},
            {name: '身份证号', text: ''},
            {name: '性别', text: ''},
            {name: '手机号', text: ''},
        ]
    },
    onLoad: function () {
        if(!app.globalData.userInfo) {
            this.getUserInfo();
        } else {
            var userInfo = app.globalData.userInfo;
            if(userInfo.sex == 1) {
                userInfo.sex = '男'
            } else if(userInfo.sex == 2) {
                userInfo.sex = '女'
            } else {
                userInfo.sex = '未选择'
            }
            var navList = [
                {name: '用户名', text: userInfo.nickname},
                {name: '真实姓名', text: userInfo.username},
                {name: '身份证号', text: userInfo.idcard},
                {name: '性别', text: userInfo.sex},
                {name: '手机号', text: userInfo.phone},
                {name: '职业', text: userInfo.job},
            ]
            this.setData({
                nav_list: navList,
            })
        }
    },
    onHide: function () {
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.getUserInfo();
    },

    /**
     * 获取用户信息
     */
    getUserInfo: function(){
        var _this = this;
        var object = {
            url: app.api.url.user.userInfo,
            success: function(res){
                if(res.status == 1) {
                    app.globalData.userInfo = res.data;
                    var userInfo = res.data;
                    if(userInfo.sex == 1) {
                        userInfo.sex = '男'
                    } else if(userInfo.sex == 2) {
                        userInfo.sex = '女'
                    } else {
                        userInfo.sex = '未选择'
                    }
                    var navList = [
                        {name: '用户名', text: userInfo.nickname},
                        {name: '真实姓名', text: userInfo.username},
                        {name: '身份证号', text: userInfo.idcard},
                        {name: '性别', text: userInfo.sex},
                        {name: '手机号', text: userInfo.phone},
                        {name: '职业', text: userInfo.job},
                    ]
                    _this.setData({
                        nav_list: navList
                    })

                } else {
                    app.showToast(res.info);
                }
            },
            complete: function() {
                wx.stopPullDownRefresh() //停止下拉刷新
            }
        }
        app.request(object);
    },
});
